<?php

	/**
	 * @see Zend_Db_Table_Row_Abstract
	 */
	require_once 'Zend/Db/Table/Row/Abstract.php';
	
	/**
	 * @see Bike_Db_Table
	 */
	require_once 'Bike/Db/Table.php';
	
	/**
	 * @see Bike_Language
	 */
	require_once 'Bike/Language.php';
	
	
	class Bike_Db_Table_Row extends Zend_Db_Table_Row_Abstract
	{
		protected function _transformColumn($columnName)
	    {
	        $columnName = parent::_transformColumn($columnName);
	        return Bike_String::factory($columnName)->stripCamelCase();
	    }

		public function __call($method, array $args)
    	{    		if(substr($method, 0, 3) == 'get')
    		{    			$columnName = substr($method, 3);
    			return $this->$columnName;
    		}
    		if(substr($method, 0, 3) == 'set')
    		{
    			$columnName = substr($method, 3);
    			$this->$columnName = reset($args);
    			return $this;
    		}
    		if(substr($method, 0, 5) == 'unset')
    		{
    			$columnName = substr($method, 5);
    			return $this->__unset($columnName);
    		}
    		if(substr($method, 0, 5) == 'isset')
    		{
    			$columnName = substr($method, 5);
    			return $this->__isset($columnName);
    		}
    		return parent::__call($method, $args);
    	}

    	public function __set($ColumnName, $Value)
		{
			$columnName = $this->_transformColumn($ColumnName);
	        if(!in_array($columnName, $this->getTable()->info(Bike_Db_Table::COLS)))
	        {
	            require_once 'Zend/Db/Table/Row/Exception.php';
	            throw new Zend_Db_Table_Row_Exception(sprintf('Specified column "%s" is not in the row or for read only', $ColumnName));
	        }
	        if(method_exists($this, 'processSet'.$ColumnName))
	        {	        	$Value = call_user_func(array($this, 'processSet'.$ColumnName), $Value);
	        }
	        $this->_data[$columnName] = $Value;
	        $this->_modifiedFields[$columnName] = true;
		}

		public function __get($ColumnName)
	    {
	        if($this->getTable()->getLocalization())
	        {
		        try
		        {		        	$value = parent::__get($ColumnName);
		        }
		        catch(Zend_Db_Table_Row_Exception $e)
		        {		        	try
		        	{
		        		$columnName = $this->_transformColumn($ColumnName);
		        		$langColumnName = $this->getTable()->getLocalizedFieldName($columnName);
		        		$value = $this->$langColumnName;
		        	}
		        	catch(Zend_Db_Table_Exception $e)
		        	{
		        		require_once 'Zend/Db/Table/Row/Exception.php';
		        		throw new Zend_Db_Table_Row_Exception(sprintf('Specified column "%s" is not in the row', $ColumnName));
		        	}
		        }
		  	}
		  	else
		  	{
		  		$value = parent::__get($ColumnName);
			}
		  	if(method_exists($this, 'processGet'.$ColumnName))
	        {
	        	$value = call_user_func(array($this, 'processGet'.$ColumnName), $value);
	        }
	        return $value;
	    }

	    public function getPrimaryKey()
	    {	    	return $this->_getPrimaryKey();
	    }
	    
	    public function getPrimaryKeyValue()
	    {
	    	$primaryKey = $this->getPrimaryKey();
	    	if(sizeof($primaryKey) == 1)
	    	{
	    		return reset($primaryKey);
	    	}
	    	return $primaryKey;
	    }
	}
